﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApp01_7
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                var isActive = Request.QueryString["cancel"];
                if (isActive != null)
                {
                    if (Convert.ToBoolean(isActive))
                    {
                        string sql = "select * from Task";
                        var num = DbHelper.GetData(sql).Rows.Count / (GridView1.PageSize * 1.0);

                        GridView1.PageIndex = Convert.ToInt32(Math.Ceiling(num));
                    }
                }

                Display();

            }

            if (Session["userInfor"] == null)
            {
                Response.Write("<script>alert('当前还未登录，请登入后访问');location.assign('Login.aspx');</script>");

            }
            else
            {
                var user = (SortedList<string, string>)Session["userInfor"];
                userInforLab.Text = user["userName"];
            }

            
        }

        public void Display()
        {
            string sql = "select * from Task";
            GridView1.DataSource = DbHelper.GetData(sql);
            GridView1.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Display();
        }



        protected void xqBtn_Click(object sender, EventArgs e)
        {

        }





        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string sql = "delete Task where id=" + id;
            var res=DbHelper.UpdateData(sql);
            if (res > 0)
            {
                Display();
                Response.Write("<script>alert('删除成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('删除失败')</script>");
            }
            
        }

        protected void addBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddTask.aspx");

        }
    }
}